<?php
class Analyser_Bracket extends AbstractAnalyser {
	private $_bracketId = 0;
	private $_curlyStack = array();
	private $_roundStack = array();
	private $_squareStack = array();

	protected function registerEvents() {
		$this->register('token.T_OPEN_CURLY_BRACKET', 'onOpenCurlyBracket');
		$this->register('token.T_CLOSE_CURLY_BRACKET', 'onCloseCurlyBracket');
		$this->register('token.T_OPEN_ROUND_BRACKET', 'onOpenRoundBracket');
		$this->register('token.T_CLOSE_ROUND_BRACKET', 'onCloseRoundBracket');
		$this->register('token.T_OPEN_SQUARE_BRACKET', 'onOpenSquareBracket');
		$this->register('token.T_CLOSE_SQUARE_BRACKET', 'onCloseSquareBracket');
	}

	public function onOpenCurlyBracket() {
		array_push($this->_curlyStack, ++$this->_bracketId);
		$this->send('curly_bracket.open', $this->_bracketId);
		Log::debug('curly open bracket : %d', $this->_bracketId);
	}

	public function onCloseCurlyBracket() {
		$id = array_pop($this->_curlyStack);
		$this->send('curly_bracket.close', $id);
		Log::debug('curly close bracket : %d', $id);
	}

	public function onOpenRoundBracket() {
		array_push($this->_roundStack, ++$this->_bracketId);
		$this->send('round_bracket.open', $this->_bracketId);
			Log::debug('round open bracket : %d', $this->_bracketId);
	}

	public function onCloseRoundBracket() {
		$id = array_pop($this->_roundStack);
		$this->send('round_bracket.close', $id);
			Log::debug('round close bracket : %d', $id);
	}

	public function onOpenSquareBracket() {
		array_push($this->_squareStack, ++$this->_bracketId);
		$this->send('square_bracket.open', $this->_bracketId);
			Log::debug('square open bracket : %d', $this->_bracketId);
	}

	public function onCloseSquareBracket() {
		$id = array_pop($this->_squareStack);
		$this->send('square_bracket.close', $id);
			Log::debug('square close bracket : %d', $id);
	}

	public function reset() {
		$this->_bracketId = 0;
		$this->_curlyStack = array();
		$this->_roundStack = array();
		$this->_squareStack = array();
	}

}